package com.wy.controller.wy;

import com.wy.model.sys.PageResult;
import com.wy.model.sys.ParkEntity;
import com.wy.service.wy.ParkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/parks")
public class ParkController {
    private Logger logger = LoggerFactory.getLogger(ParkController.class);
    @Resource(name = "parkServiceImpl")
    private ParkService parkService;

    /**
     * 车场列表
     * @param pageSize
     * @param page
     * @param type
     * @param status
     * @param residenceId
     * @return
     */
    @GetMapping("")
    public PageResult parkList(@RequestParam(value = "pageSize",required = false) int pageSize,
                               @RequestParam(value = "page",required = false)int page,
                               @RequestParam(value = "type",required = false)String type,
                               @RequestParam(value = "status",required = false)String status,
                               @RequestParam(value = "residenceId",required = false)String residenceId,
                               @RequestParam(value = "parkName",required = false) String parkName){
        PageResult pageResult = new PageResult();
        pageResult.setData(parkService.parkList(pageSize,page*pageSize,type,status,residenceId,parkName));
        pageResult.setTotalCount(parkService.parkSize(pageSize,page*pageSize,type,status,residenceId,parkName));
        return pageResult;
    }

    /**
     * 新增车场
     * @param parkEntity
     * @return
     */
    @PostMapping("/park")
    public ParkEntity insertPark(@RequestBody ParkEntity parkEntity){
        parkEntity.setStatus("1");
        parkService.insertPark(parkEntity);
        logger.debug("the method ending");
        return parkEntity;
    }

    /**
     * 修改车场信息
     * @param parkEntity
     * @param id
     * @return
     */
    @PutMapping("{id}")
    public ParkEntity updatePark(@RequestBody ParkEntity parkEntity,@PathVariable int id){
        if(parkEntity.getId() ==id){
            parkService.updatePark(parkEntity);
        }
        return parkEntity;
    }

    @DeleteMapping("/del")
    public void delpark(@RequestBody List<String> groupId){
        parkService.deletePark(groupId);
    }

    @GetMapping("/getPark_residenceId")
    public  PageResult  getParkByResidenceId(@RequestParam("residenceId") String residenceId ){
       PageResult pageResult = new PageResult();
       pageResult.setData(parkService.getParkByResidenceId(residenceId));
       pageResult.setTotalCount(parkService.parkSize(0,0,null,null,residenceId,null));
       return pageResult;
    }
}
